<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends MY_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('member_model');
	}
	
	function index(){
		
		//必要参数检测
		if(empty($this->p['id'])) $this->page_to_index();
		$id = $this->p['id'];
		
		//会员信息
		$member = $this->cm->get_by_id($this->tbl->member,$id);
		if(empty($member->id)) $this->page_to_index();

		$this->_data['member'] = $member;
	
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
		$this->load->model('topic_model');
		
		//会员发表的主题
		$this->p['poster_id'] = $id;
		$list_topic = $this->topic_model->get_list_by_page($limit_num,$this->p);
		$loginer_id = empty($this->loginer->id) ? 0 : $this->loginer->id;
		$list_topic = $this->topic_model->list_favo_status($list_topic,$loginer_id);
		$this->_data['list_topic'] = $list_topic; 
		
	    //分页导航
    	$this->pagination->base_url = $this->page_url($this->p);;
	    $this->pagination->total_rows = $this->topic_model->count;
	    $this->_data['pagelinks'] = $this->pagination->create_links();

	    //发表的主题数
	    $this->_data['topic_count'] = $this->topic_model->count;
	    

	    //是否关注了此会员
	    $is_followed = false;
	    if($this->is_login && $this->loginer->id != $id){
	    	if($this->cm->in($this->tbl->follow,array('uid'=>$this->loginer->id,'fid'=>$id))) $is_followed = true;
	    }
	    $this->_data['is_followed'] = $is_followed;
	    
	    //是不是自己
	    $is_self = false;
	    if($this->is_login && $this->loginer->id == $id){
	    	$is_self = true;
	    }
	    $this->_data['is_self'] = $is_self;
	    
		$this->load->view('member/index',$this->_data);

	}
	
	public function login(){
	    $this->session->set_userdata('scrollpage',1);
		$this->load->view('member/login',$this->_data);
	}
	
	public function login_do(){

	    $email = $this->p['email'];
		$upass = $this->p['upass'];
		
		//用户数据验证
		if(!$this->form_validation->required($email)) ejd('fail', '错误的用户名或密码！');
		if(!$this->form_validation->required($upass)) ejd('fail', '错误的用户名或密码！');
		
		//登录
		if($this->member_model->login($email,$upass)){
			ejd('succ', '', site_url());
		}else{
			ejd('fail', '错误的用户名或密码！');
		}
	}
	
	function loginout(){
		$this->session->sess_destroy();
		redirect(site_url());
	}

	function regist(){
	    if($this->is_login){
	        redirect(site_url());
	    }
	    if(!empty($this->p['invitor'])){
		    $this->_data['invitor'] = $this->p['invitor'];
	    }
		$this->load->view('member/regist',$this->_data);
	}
	
	function regist_do(){
		
		//用户数据验证
		//**空
		if(!$this->form_validation->required($this->p['nname'])) die;
		if(!$this->form_validation->required($this->p['email'])) die;
		if(!$this->form_validation->required($this->p['upass'])) die;
		if(!$this->form_validation->required($this->p['sex'])) die;
		
		//**格式
		if(!validation_upass($this->p['upass'])) die;
		if(!$this->form_validation->valid_email($this->p['email'])) die;
		if(!in_array($this->p['sex'], array('male','female','ycmale','jjmale'))) die;
		
		//注册
		$this->p['upass'] = md5($this->p['upass']);
		$flag = $this->member_model->regist($this->p);
		
		if($flag == -2){
			ejd('fail','相同的邮箱已存在！');
		}elseif($flag == -1){
			ejd('fail','相同的昵称已存在！');
		}elseif($flag == 0){
			ejd('fail','发生异常请稍后再试！');
		}
		
		ejd('succ','操作成功！',site_url('c=member&m=login'));
		
	}
	
	function follow(){
		
		//登录检测
		$this->check_ajax_login();
		
		//数据校验
		$id = $this->p['id'];
		if(empty($id)) die;
		if($this->loginer->id == $id) ejd('unlogin');
		if(!$this->cm->in('member',array('id'=>$id))) die;

		$cond = array('uid'=>$this->loginer->id,'fid'=>$id);
		if($this->cm->in('follow',$cond)){
			$this->cm->delete('follow',$cond);
			echo json_encode(array('status'=>'unfollowed'));
		}else{
			$this->cm->insert('follow',$cond);
			echo json_encode(array('status'=>'followed'));
		}
		exit;
	}
	
	function ajax_login(){
		echo $this->load->view('member/ajax_login',$this->_data,true);
	}
	
	public function ajax_login_do(){

		$email = $this->p['email'];
		$upass = $this->p['upass'];
		
		//用户数据验证
		if(!$this->form_validation->required($email)) die;
		if(!$this->form_validation->required($upass)) die;
		
		//登录
		if($this->member_model->login($email,$upass)){
			ejd('', '', 'reload');
		}else{
			ejd('fail', '错误的用户名或密码！');
		}
	}
	
	function msg(){
        if(!$this->is_login){
            die('登录超时，请重新登录！');
        }
        
        if(empty($this->p['id'])) die;
        $this->_data['id'] = $this->p['id'];
        echo $this->load->view('member/ajax_msg',$this->_data,true);        
	}
	
	public function msg_do(){
	    $this->check_ajax_login();
	    if(empty($this->p['to_id'])) die;
	    if(empty($this->p['cont'])) die;
        $this->p['from_id'] = $this->loginer->id;
	    $this->cm->save($this->tbl->msg,$this->p);
	    
	    ejd('succ');
	}
	
	
	
}

